package com.ewhale.feitengguest.ui.auth;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.ui.user.MainActivity;
import com.ewhale.feitengguest.ui.shop.ShopMainActivity;
import com.ewhale.feitengguest.utils.Contants;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.base.IPresenter;
import com.simga.library.base.IView;
import com.simga.library.utils.HawkKey;
import com.simga.library.widget.BGButton;

import java.util.ArrayList;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : zhouyx
 * Date   : 2017/3/15
 * Description : 引导页
 */
public class GuideActivity extends MBaseActivity<IPresenter> implements IView {

    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    @BindView(R.id.btn_confirm)
    BGButton btnConfirm;
//    @BindView(R.id.view1)
//    View view1;
//    @BindView(R.id.view2)
//    View view2;
//    @BindView(R.id.view3)
//    View view3;
//    @BindView(R.id.ll_circle)
//    LinearLayout llCircle;

    /**
     * 引导页图片
     */
    private Integer[] pages = new Integer[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
    private ArrayList<View> viewList;

    @Override
    protected void onGetBundle(Bundle bundle) {
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        viewList = new ArrayList<>();
        GuidePageAdapter adapter = new GuidePageAdapter();
        mViewPager.setAdapter(adapter);
        for (Integer page : pages) {
            ImageView iv = new ImageView(mContext);
            iv.setImageResource(page);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);//图片按等比缩放
            viewList.add(iv);
        }
    }

    @Override
    public void initListener() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
//                if (position == 0) {
//                    view1.setBackgroundResource(R.drawable.draw_cirle_white_3);
//                    view2.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                    view3.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                } else if (position == 1) {
//                    view1.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                    view2.setBackgroundResource(R.drawable.draw_cirle_white_3);
//                    view3.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                } else if (position == 2) {
//                    view1.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                    view2.setBackgroundResource(R.drawable.draw_cirle_white_2);
//                    view3.setBackgroundResource(R.drawable.draw_cirle_white_3);
//                }
                if (position == (pages.length - 1)) {//最后一页
                    btnConfirm.setVisibility(View.VISIBLE);
//                    llCircle.setVisibility(View.GONE);
                } else {
                    btnConfirm.setVisibility(View.GONE);
//                    llCircle.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @OnClick({R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                Hawk.put(HawkKey.FIRST_IN, false);
                if(Hawk.get(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS).equals(Contants.SHOP_STATUS)){
                    ShopMainActivity.open(mContext);
                }else{
                    MainActivity.open(mContext);
                }
                finish();
                break;
        }
    }
    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    private class GuidePageAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return pages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));//删除页卡
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    }
}
